﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Teigha.DatabaseServices;
using Teigha.Geometry;
using Teigha.Colors;
using Teigha.Export_Import;
using Teigha.GraphicsInterface;
using Teigha.GraphicsSystem;
using Teigha.Runtime;


namespace ExportToDWGTool
{
    class CreateDWG
    {
        //创建Teigha服务
        class KWD
        {
            public Point3d pt;  //孔位点坐标
            public string str;  //孔位编号
        }
       
        public Dictionary<string, JingInfo> m_DicJ = null;
        public Dictionary<string, SheBeiInfo> m_DicSB = null;

        public Dictionary<string, Dictionary<int, Dictionary<int, SheBeiInfo>>> m_dicDLDTD = null;

        Dictionary<string, ObjectId> m_dicSelBlk = new Dictionary<string,ObjectId>();

        public Dictionary<string, string> m_dicDLD = null;

        public Database m_pDb = null;

        public int m_nCount = 0;


        public bool SaveToDWG(string strTempDWG,string strOutSave)
        {
            try
            {
                System.DateTime time = System.DateTime.Now;
                // LogManageMt.m_listLog.Add("时间：" + time.ToString() + "   开始转换，请等待……");

                using (Database pDb = new Database(false, false))// 加参数出错
                {
                    pDb.ReadDwgFile(strTempDWG,FileOpenMode.OpenForReadAndWriteNoShare, false, "");

                    // LogManageMt.m_listLog.Add("开始将转换对象添加到模型空间，请等待……");
                    m_pDb = pDb;

                    BlockTableRecord btr = (BlockTableRecord)pDb.CurrentSpaceId.GetObject(OpenMode.ForWrite);
                    //绘制井参照块
                    foreach(string key in m_DicJ.Keys)
                    {
                        JingInfo pInfo = m_DicJ[key];

                        //if (pInfo.w_MC != "智慧三街Ⅰ段36号井")
                        //{
                        //    continue;
                        //}

                        ObjectId newBlkId = CreateWellBlk(pDb, pInfo);
                        if (newBlkId == ObjectId.Null)
                            continue;

                        double x = 0;
                        double y = 0;
      
                        ConvertPlaneCoord.MCT84Bl2xy(double.Parse(pInfo.w_X), double.Parse(pInfo.w_Y), out x, out y);
                        Point2d pt = new Point2d(y,x);
                 
                        //添加井
                        ObjectId BlkId = ObjectId.Null;
                        if (m_dicSelBlk.Keys.Contains(pInfo.w_JLB))
                        {
                            BlkId = m_dicSelBlk[pInfo.w_JLB];    
                        }
                        else
                        {
                            BlkId = UtilityFunc.GetBlkIdByName(pDb, pInfo.w_JLB);
                            m_dicSelBlk.Add(pInfo.w_JLB, BlkId);
                        }
                        if(BlkId!=null)
                        {
                            BlockReference blkRef = new BlockReference(new Point3d(y,x,0), BlkId);
                            ObjectId wId = btr.AppendEntity(blkRef);
                         
                            Entity wEnt = (Entity)wId.Open(OpenMode.ForWrite);
                            wEnt.Layer = "剖面图";
                        }

                        ObjectId Id = ObjectId.Null;
                        Entity pwEnt = null;

                        Teigha.DatabaseServices.Polyline pLine = new Teigha.DatabaseServices.Polyline();
                        pLine.AddVertexAt(0,pt,0,0,0);
                        Point2d pt1 = new Point2d(y+4.5, x+4.5);
                        pLine.AddVertexAt(1, pt1, 0, 0, 0);
                        Id = btr.AppendEntity(pLine);
                        pwEnt = (Entity)Id.Open(OpenMode.ForWrite);
                        pwEnt.Layer = "剖面图";

                        Point3d pt3dText = new Point3d(y + 2.5, x + 2.5, 0);
                        DBText pText = CreateText(pt3dText, pInfo.w_MC, "井注记7");
                        pText.VerticalMode = TextVerticalMode.TextBottom;
                        pText.HorizontalMode = TextHorizontalMode.TextCenter;
                        pText.Height = 0.5;
                        pText.Rotation = 0.8;

                        Id = btr.AppendEntity(pText);
                        pwEnt = (Entity)Id.Open(OpenMode.ForWrite);
                        pwEnt.Layer = "电缆井名称";
                        pwEnt.Color = Color.FromColor(System.Drawing.Color.Yellow);

                        using (BlockTableRecord pBlock = (BlockTableRecord)newBlkId.Open(OpenMode.ForRead))
                        {
                            Point3d pt3D = new Point3d(y + 15.06, x + 11.83, 0);
                            BlockReference blkRef = new BlockReference(pt3D, newBlkId);
                            Id = btr.AppendEntity(blkRef);
                            pwEnt = (Entity)Id.Open(OpenMode.ForWrite);
                            pwEnt.Layer = "剖面图";
                        }
                    }

                    double X = 0;
                    double Y = 0;
                    //添加设备
                    foreach (string strID in m_DicSB.Keys)
                    {
                        SheBeiInfo pInfo = m_DicSB[strID];

                        ConvertPlaneCoord.MCT84Bl2xy(double.Parse(pInfo.s_X), double.Parse(pInfo.s_Y), out X, out Y);
                      
                        ObjectId BlkId = ObjectId.Null;
                        if (m_dicSelBlk.Keys.Contains(pInfo.s_Type))
                        {
                            BlkId = m_dicSelBlk[pInfo.s_Type];
                        }
                        else
                        {
                            BlkId = UtilityFunc.GetBlkIdByName(pDb, pInfo.s_Type);
                            m_dicSelBlk.Add(pInfo.s_Type, BlkId);
                        }
                        if (BlkId != null)
                        {
                            BlockReference blkRef = new BlockReference(new Point3d(Y, X, 0), BlkId);
                            ObjectId wId = btr.AppendEntity(blkRef);

                            Entity wEnt = (Entity)wId.Open(OpenMode.ForWrite);
                            wEnt.Layer = "电力设备";
                        }

                        Point3d pt3dText = new Point3d(Y, X + 2.5, 0);
                        DBText pText = CreateText(pt3dText, pInfo.s_Name, "井注记7");
                        pText.VerticalMode = TextVerticalMode.TextBottom;
                        pText.HorizontalMode = TextHorizontalMode.TextCenter;
                        pText.Height = 1;
                        pText.Rotation = 0;

                        ObjectId Id = btr.AppendEntity(pText);
                        Entity pwEnt = (Entity)Id.Open(OpenMode.ForWrite);
                        pwEnt.Layer = "电缆井名称";
                        pwEnt.Color = Color.FromColor(System.Drawing.Color.Yellow);


                    } 

       
                    //绘制通道
                    //foreach (string strID in m_DicDLD.Keys)
                    //{
                    //    Dictionary<string, SheBeiInfo> dicSB = m_DicDLD[strID];

                    //    Teigha.DatabaseServices.Polyline pLine = CreateTongDaoX(pDb,dicSB, btr);

                    //    btr.AppendEntity(pLine);

                       
                    //}

                    foreach (string strID in m_dicDLDTD.Keys)
                    {

                        Dictionary<int, Dictionary<int, SheBeiInfo>> dicTD = m_dicDLDTD[strID];

                       //// Dictionary<int, Dictionary<int, SheBeiInfo>> dicTDAsc = dicTD.OrderBy(o => o.Key).ToDictionary(o => o.Key, p => p.Value);

                        //Dictionary<int, Dictionary<int, SheBeiInfo>> dicTDdesc = dicTD.OrderByDescending(o => o.Key).ToDictionary(o => o.Key, p => p.Value);

 
                        string str = "";
                        if (m_dicDLD.Keys.Contains(strID))
                        {
                            str = m_dicDLD[strID];
                            str.Replace(" ", "");
                            if (str.Contains("("))
                            {
                                str = str.Substring(0, str.IndexOf('('));
                            }
                            else if (str.Contains("（"))
                            {
                                str = str.Substring(0, str.IndexOf('（'));
                            }
                        
                        }

                        CreateTongDaoX(pDb, str, dicTD, btr);

                    }



                    if (m_DicJ.Count > 0)
                    {
                        SaveType pSavetype = SaveType.Save12; //Save12 为.dwg Save13 为dxf

                        DwgVersion dwgver = DwgVersion.vAC21; //ACAD2010为 vAC24； ACAD2007 为 vAC21； ACAD2004为vAC18；

                        pDb.SaveAs(strOutSave, dwgver);
                    }

                    time = System.DateTime.Now;
                    

                }

                return true;
            }
            catch (System.Exception ex)
            {
                System.DateTime time = System.DateTime.Now;
               
                MessageBox.Show(ex.Message);

                return false;
            }
        }


        /// <summary>
        /// wcy修改
        /// </summary>
        /// <param name="pDb"></param>
        /// <param name="pWInfo"></param>
        /// <returns></returns>
        /// 
        /// <summary>
        /// wcy修改
        /// </summary>
        /// <param name="pDb"></param>
        /// <param name="pWInfo"></param>
        /// <returns></returns>
        private ObjectId CreateWellBlk(Database pDb, JingInfo pWInfo)
        {
            ObjectId newBlkId = ObjectId.Null;

            try
            {
                using (BlockTable blockTable = (BlockTable)pDb.BlockTableId.GetObject(OpenMode.ForWrite))
                {
                    ObjectId wBlockId;
                    using (BlockTableRecord btr = new BlockTableRecord())
                    {
                        m_nCount++;
                        btr.Name = "W" + m_nCount.ToString(); //pWInfo.w_MC;
                        if (blockTable.Has(btr.Name))
                        {
                            btr.Name = "1" + btr.Name;
                        }
                        wBlockId = blockTable.Add(btr);
                        if (blockTable.Has("TT"))
                        {
                            ObjectId BlkId = ObjectId.Null;
                            if (m_dicSelBlk.Keys.Contains("TT"))
                            {
                                BlkId = m_dicSelBlk["TT"];
                            }
                            else
                            {
                                BlkId = UtilityFunc.GetBlkIdByName(pDb, "TT");
                                m_dicSelBlk.Add("TT", BlkId);
                            }

                            if (BlkId != null)
                            {
                                //添加井属性
                                CreateAttribute(pDb, pWInfo, btr);
                                //创建外框
                                Point3d pt3D = new Point3d(0, 0, 0);
                                BlockReference blkRef = new BlockReference(pt3D, BlkId);
                                btr.AppendEntity(blkRef);

                                //创建指北针

                                ObjectId zbzId = ObjectId.Null;
                                if (m_dicSelBlk.Keys.Contains("ZBZ"))
                                {
                                    zbzId = m_dicSelBlk["ZBZ"];
                                }
                                else
                                {
                                    zbzId = UtilityFunc.GetBlkIdByName(pDb, "ZBZ");
                                    m_dicSelBlk.Add("ZBZ", zbzId);
                                }

                                blkRef = new BlockReference(pt3D, zbzId);
                                btr.AppendEntity(blkRef);

                                //创建井
                                if (pWInfo.w_JC != "" && pWInfo.w_JK != "" && pWInfo.w_JS != "")
                                {
                                    double L = double.Parse(pWInfo.w_JC) / 1000;
                                    double W = double.Parse(pWInfo.w_JK) / 1000;
                                    double H = double.Parse(pWInfo.w_JS) / 1000;

                                    double OffX = L / 2;
                                    double OffY = W / 2;
                                    Teigha.DatabaseServices.Polyline pLWell = CreatePLine(L, W, -OffX, OffY);

                                    ObjectId wId = btr.AppendEntity(pLWell);
                                    Entity pwEnt = (Entity)wId.Open(OpenMode.ForWrite);
                                    pwEnt.Layer = "井剖面";

                                    pwEnt.Color = Color.FromColor(System.Drawing.Color.Lime);

                                    //井长度标注
                                    Point3d ptStart = new Point3d(-OffX, OffY, 0);
                                    Point3d ptEnd = new Point3d(L - OffX, OffY, 0);
                                    Point3d ptDimLine = new Point3d(L - OffX, OffY + 0.1, 0);
                                    AlignedDimension pAligDimL = CreateAlignedDim(ptStart, ptEnd, ptDimLine, pWInfo.w_JC.ToString());
                                    btr.AppendEntity(pAligDimL);

                                    //井宽度标注
                                    ptStart = new Point3d(L - OffX, OffY, 0);
                                    ptEnd = new Point3d(L - OffX, -W + OffY, 0);
                                    ptDimLine = new Point3d(L - OffX + 0.1, -W + OffY, 0);
                                    AlignedDimension pAligDimW = CreateAlignedDim(ptStart, ptEnd, ptDimLine, pWInfo.w_JK.ToString());
                                    btr.AppendEntity(pAligDimW);

                                    //创建断面

                                    Dictionary<string, string> dicTP = new Dictionary<string, string>();
                                    Dictionary<string, ObjectId> dicBlk = new Dictionary<string, ObjectId>();
                                    double dJJ = 0.3;//间距
                                    foreach (string key in pWInfo.m_DicDM.Keys)
                                    {
                                        DuanMianInfo pSInfo = pWInfo.m_DicDM[key];
                                        Teigha.DatabaseServices.Polyline pSWell = null;

                                        if (!dicTP.Keys.Contains(pSInfo.s_FW))
                                        {
                                            dicTP.Add(pSInfo.s_FW, pSInfo.s_TP);
                                        }

                                        if (pSInfo.s_FW == "北")
                                        {
                                            //创建井断面
                                            OffX = L / 2;
                                            OffY = H + W / 2 + dJJ;
                                            pSWell = CreatePLine(L, H, -OffX, OffY);

                                            //上边距标注
                                            double dS = double.Parse(pSInfo.s_SBJ) / 1000;
                                            if (dS > 0)
                                            {
                                                Point3d ptS0 = new Point3d(L - OffX, OffY, 0);
                                                Point3d ptS1 = new Point3d(L - OffX, OffY - dS, 0);
                                                Point3d ptDL = new Point3d(L - OffX + 0.1, OffY, 0);
                                                AlignedDimension pAligDimS = CreateAlignedDim(ptS0, ptS1, ptDL, pSInfo.s_SBJ.ToString());
                                                btr.AppendEntity(pAligDimS);
                                            }

                                            //下边距标注
                                            double dX = double.Parse(pSInfo.s_XBJ) / 1000;
                                            if (dX > 0)
                                            {
                                                Point3d ptX0 = new Point3d(L - OffX, -H + OffY, 0);
                                                Point3d ptX1 = new Point3d(L - OffX, -H + OffY + dX, 0);
                                                Point3d ptDL = new Point3d(L - OffX + 0.1, -H + OffY, 0);
                                                AlignedDimension pAligDimX = CreateAlignedDim(ptX0, ptX1, ptDL, pSInfo.s_XBJ.ToString());
                                                btr.AppendEntity(pAligDimX);
                                            }

                                            //左边距标注
                                            double dZ = double.Parse(pSInfo.s_ZBJ) / 1000;
                                            if (dZ > 0)
                                            {
                                                Point3d ptZ0 = new Point3d(-OffX, OffY, 0);
                                                Point3d ptZ1 = new Point3d(-OffX + dZ, OffY, 0);
                                                Point3d ptDL = new Point3d(-OffX, OffY + 0.1, 0);
                                                AlignedDimension pAligDimZ = CreateAlignedDim(ptZ0, ptZ1, ptDL, pSInfo.s_ZBJ.ToString());
                                                btr.AppendEntity(pAligDimZ);
                                            }
                                            //右边距标注
                                            double dY = double.Parse(pSInfo.s_YBJ) / 1000;
                                            if (dY > 0)
                                            {
                                                Point3d ptY0 = new Point3d(L - OffX, OffY, 0);
                                                Point3d ptY1 = new Point3d(L - OffX - dY, OffY, 0);
                                                Point3d ptDL = new Point3d(L - OffX, OffY + 0.1, 0);
                                                AlignedDimension pAligDimY = CreateAlignedDim(ptY0, ptY1, ptDL, pSInfo.s_YBJ.ToString());
                                                btr.AppendEntity(pAligDimY);
                                            }
                                            //添加孔位
                                            if (pSInfo.m_DicKW != null && pSInfo.m_DicKW.Count > 0)
                                            {

                                                double hJJ = (double.Parse(pWInfo.w_JS) - (int.Parse(pSInfo.s_H) * pSInfo.s_maxGJ) - double.Parse(pSInfo.s_SBJ) - double.Parse(pSInfo.s_XBJ)) / (int.Parse(pSInfo.s_H) - 1);
                                                double lJJ = (double.Parse(pWInfo.w_JC) - (int.Parse(pSInfo.s_L) * pSInfo.s_maxGJ) - double.Parse(pSInfo.s_ZBJ) - double.Parse(pSInfo.s_YBJ)) / (int.Parse(pSInfo.s_L) - 1);
                                                hJJ = hJJ / 1000;
                                                lJJ = lJJ / 1000;
                                                if (pSInfo.s_H == "1")
                                                {
                                                    hJJ = 0;
                                                }
                                                if (pSInfo.s_L == "1")
                                                {
                                                    lJJ = 0;
                                                }

                                                //孔位标注 孔位标注左边朝左，右边朝右
                                                double xOff = 4;
                                                double yOff = 0.5;

                                                //先绘制列
                                                double x0 = -OffX + double.Parse(pSInfo.s_ZBJ) / 1000;
                                                double y0 = OffY - double.Parse(pSInfo.s_SBJ) / 1000;
                                                double dGJ = pSInfo.s_maxGJ / 2000;

                                                for (int m = 0; m <= int.Parse(pSInfo.s_L); m++)
                                                {
                                                    y0 = OffY - double.Parse(pSInfo.s_SBJ) / 1000;
                                                    x0 = x0 + dGJ;
                                                    xOff = xOff + dGJ;

                                                    for (int n = 0; n <= int.Parse(pSInfo.s_H); n++)
                                                    {
                                                        y0 = y0 - dGJ;

                                                        string strCurHL = n.ToString() + "_" + m.ToString();
                                                        if (!pSInfo.m_DicKW.Keys.Contains(strCurHL))
                                                        {
                                                            y0 = y0 - dGJ - hJJ;
                                                            continue;
                                                        }

                                                        KongWeiInfo pHInfo = pSInfo.m_DicKW[strCurHL];
                                                        if (pHInfo.h_GJ == "")
                                                        {
                                                            y0 = y0 - dGJ - hJJ;
                                                            continue;
                                                        }

                                                        Point3d ptKW = new Point3d(x0, y0, 0);

                                                        string strName = pHInfo.h_LX + pHInfo.h_GJ;
                                                        ObjectId KId = ObjectId.Null;
                                                        if (m_dicSelBlk.Keys.Contains(strName))
                                                        {
                                                            KId = m_dicSelBlk[strName];
                                                        }
                                                        else
                                                        {
                                                            KId = UtilityFunc.GetBlkIdByName(pDb, strName);
                                                            m_dicSelBlk.Add(strName, KId);
                                                        }

                                                        if (KId == ObjectId.Null)
                                                        {
                                                            y0 = y0 - dGJ - hJJ;
                                                            continue;
                                                        }
                                                        BlockReference blkRefKW = new BlockReference(ptKW, KId);
                                                        btr.AppendEntity(blkRefKW);

                                                        string strLead = pHInfo.h_LX + pHInfo.h_CZ + pHInfo.h_GJ;
                                                        if (!dicBlk.Keys.Contains(strLead))
                                                        {
                                                            dicBlk.Add(strLead, KId);
                                                        }

                                                        if (pHInfo.h_XCBH != ""&& pHInfo.h_XCBH != "无")
                                                        {
                                                            Point2dCollection ptColt = new Point2dCollection();
                                                            ptColt.Add(new Point2d(x0, y0));
                                                            ptColt.Add(new Point2d(x0, OffY + yOff));
                                                            ptColt.Add(new Point2d(x0 - xOff, OffY + yOff));

                                                            Teigha.DatabaseServices.Polyline pLBH = CreatePLineKW(ptColt);
                                                            btr.AppendEntity(pLBH);

                                                            DBText textBH = CreateText(new Point3d(x0 - xOff, OffY + yOff + 0.10, 0), pHInfo.h_XCBH, "井注记7");
                                                            textBH.HorizontalMode = TextHorizontalMode.TextLeft;
                                                            btr.AppendEntity(textBH);

                                                            yOff = yOff + 0.3;
                                                        }

                                                        y0 = y0 - dGJ - hJJ;

                                                    }
                                                    x0 = x0 + dGJ + lJJ;
                                                    xOff = xOff + dGJ + lJJ;
                                                }

                                            }

                                        }
                                        else if (pSInfo.s_FW == "南")
                                        {
                                            OffX = L / 2;
                                            OffY = W / 2 + dJJ;
                                            pSWell = CreatePLine(L, H, -OffX, -OffY);

                                            //上边距标注
                                            double dS = double.Parse(pSInfo.s_XBJ) / 1000;
                                            if (dS > 0)
                                            {
                                                Point3d ptS0 = new Point3d(L - OffX, -OffY, 0);
                                                Point3d ptS1 = new Point3d(L - OffX, -OffY - dS, 0);
                                                Point3d ptDL = new Point3d(L - OffX + 0.1, -OffY, 0);
                                                AlignedDimension pAligDimS = CreateAlignedDim(ptS0, ptS1, ptDL, pSInfo.s_XBJ.ToString());
                                                btr.AppendEntity(pAligDimS);
                                            }

                                            //下边距标注
                                            double dX = double.Parse(pSInfo.s_SBJ) / 1000;
                                            if (dX > 0)
                                            {
                                                Point3d ptX0 = new Point3d(L - OffX, -H - OffY, 0);
                                                Point3d ptX1 = new Point3d(L - OffX, -H - OffY + dX, 0);
                                                Point3d ptDL = new Point3d(L - OffX + 0.1, -H - OffY, 0);
                                                AlignedDimension pAligDimX = CreateAlignedDim(ptX0, ptX1, ptDL, pSInfo.s_SBJ.ToString());
                                                btr.AppendEntity(pAligDimX);
                                            }

                                            //左边距标注
                                            double dZ = double.Parse(pSInfo.s_YBJ) / 1000;
                                            if (dZ > 0)
                                            {
                                                Point3d ptZ0 = new Point3d(-OffX, -H - OffY, 0);
                                                Point3d ptZ1 = new Point3d(-OffX + dZ, -H - OffY, 0);
                                                Point3d ptDL = new Point3d(-OffX, -H - OffY - 0.1, 0);
                                                AlignedDimension pAligDimZ = CreateAlignedDim(ptZ0, ptZ1, ptDL, pSInfo.s_YBJ.ToString());
                                                btr.AppendEntity(pAligDimZ);
                                            }
                                            //右边距标注
                                            double dY = double.Parse(pSInfo.s_ZBJ) / 1000;
                                            if (dY > 0)
                                            {
                                                Point3d ptY0 = new Point3d(L - OffX, -H - OffY, 0);
                                                Point3d ptY1 = new Point3d(L - OffX - dY, -H - OffY, 0);
                                                Point3d ptDL = new Point3d(L - OffX, -H - OffY - 0.1, 0);
                                                AlignedDimension pAligDimY = CreateAlignedDim(ptY0, ptY1, ptDL, pSInfo.s_ZBJ.ToString());
                                                btr.AppendEntity(pAligDimY);
                                            }

                                            //添加孔位
                                            if (pSInfo.m_DicKW != null && pSInfo.m_DicKW.Count > 0)
                                            {

                                                double hJJ = (double.Parse(pWInfo.w_JS) - (int.Parse(pSInfo.s_H) * pSInfo.s_maxGJ) - double.Parse(pSInfo.s_SBJ) - double.Parse(pSInfo.s_XBJ)) / (int.Parse(pSInfo.s_H) - 1);
                                                double lJJ = (double.Parse(pWInfo.w_JC) - (int.Parse(pSInfo.s_L) * pSInfo.s_maxGJ) - double.Parse(pSInfo.s_ZBJ) - double.Parse(pSInfo.s_YBJ)) / (int.Parse(pSInfo.s_L) - 1);
                                                hJJ = hJJ / 1000;
                                                lJJ = lJJ / 1000;
                                                if (pSInfo.s_H == "1")
                                                {
                                                    hJJ = 0;
                                                }
                                                if (pSInfo.s_L == "1")
                                                {
                                                    lJJ = 0;
                                                }

                                                double x0 = -OffX + double.Parse(pSInfo.s_YBJ) / 1000;
                                                double y0 = -OffY - double.Parse(pSInfo.s_XBJ) / 1000;
                                                double dGJ = pSInfo.s_maxGJ / 2000;

                                                //孔位标注
                                                double xOff = 4;
                                                double yOff = 0.5;

                                                for (int m = 0; m < int.Parse(pSInfo.s_L); m++)
                                                {
                                                    y0 = -OffY - double.Parse(pSInfo.s_XBJ) / 1000;
                                                    x0 = x0 + dGJ;
                                                    xOff = xOff + dGJ;

                                                    for (int n = int.Parse(pSInfo.s_H) - 1; n >= 0; n--)
                                                    {
                                                        y0 = y0 - dGJ;

                                                        string strCurHL = n.ToString() + "_" + m.ToString();
                                                        if (!pSInfo.m_DicKW.Keys.Contains(strCurHL))
                                                        {
                                                            y0 = y0 - dGJ - hJJ;
                                                            continue;
                                                        }

                                                        KongWeiInfo pHInfo = pSInfo.m_DicKW[strCurHL];
                                                        if (pHInfo.h_GJ == "")
                                                        {
                                                            y0 = y0 - dGJ - hJJ;
                                                            continue;
                                                        }

                                                        Point3d ptKW = new Point3d(x0, y0, 0);
                                                        string strName = pHInfo.h_LX + pHInfo.h_GJ;
                                                        ObjectId KId = ObjectId.Null;
                                                        if (m_dicSelBlk.Keys.Contains(strName))
                                                        {
                                                            KId = m_dicSelBlk[strName];
                                                        }
                                                        else
                                                        {
                                                            KId = UtilityFunc.GetBlkIdByName(pDb, strName);
                                                            m_dicSelBlk.Add(strName, KId);
                                                        }

                                                        if (KId == ObjectId.Null)
                                                        {
                                                            y0 = y0 - dGJ - hJJ;
                                                            continue;
                                                        }
                                                        BlockReference blkRefKW = new BlockReference(ptKW, KId);
                                                        btr.AppendEntity(blkRefKW);

                                                        string strLead = pHInfo.h_LX + pHInfo.h_CZ + pHInfo.h_GJ;
                                                        if (!dicBlk.Keys.Contains(strLead))
                                                        {
                                                            dicBlk.Add(strLead, KId);
                                                        }

                                                        //绘制孔位标注

                                                        if (pHInfo.h_XCBH != "" && pHInfo.h_XCBH != "无")
                                                        {

                                                            Point2dCollection ptColt = new Point2dCollection();
                                                            ptColt.Add(new Point2d(x0, y0));
                                                            ptColt.Add(new Point2d(x0, -OffY - H - yOff));
                                                            ptColt.Add(new Point2d(x0 - xOff, -OffY - H - yOff));

                                                            Teigha.DatabaseServices.Polyline pLBH = CreatePLineKW(ptColt);
                                                            DBText textBH = CreateText(new Point3d(x0 - xOff, -OffY - H - yOff + 0.10, 0), pHInfo.h_XCBH, "井注记7");
                                                            btr.AppendEntity(pLBH);
                                                            textBH.HorizontalMode = TextHorizontalMode.TextLeft;
                                                            btr.AppendEntity(textBH);

                                                            yOff = yOff + 0.3;
                                                        }

                                                        y0 = y0 - dGJ - hJJ;

                                                    }
                                                    x0 = x0 + dGJ + lJJ;

                                                    xOff = xOff + dGJ + lJJ;

                                                }
                                            }

                                        }
                                        else if (pSInfo.s_FW == "东")
                                        {
                                            OffX = L / 2 + dJJ;
                                            OffY = W / 2;
                                            pSWell = CreatePLine(H, W, OffX, OffY);

                                            //上边距标注
                                            double dS = double.Parse(pSInfo.s_ZBJ) / 1000;
                                            if (dS > 0)
                                            {
                                                Point3d ptS0 = new Point3d(H + OffX, OffY, 0);
                                                Point3d ptS1 = new Point3d(H + OffX, OffY - dS, 0);
                                                Point3d ptDL = new Point3d(H + OffX + 0.1, OffY, 0);
                                                AlignedDimension pAligDimS = CreateAlignedDim(ptS0, ptS1, ptDL, pSInfo.s_ZBJ.ToString());
                                                btr.AppendEntity(pAligDimS);
                                            }

                                            //下边距标注
                                            double dX = double.Parse(pSInfo.s_YBJ) / 1000;
                                            if (dX > 0)
                                            {
                                                Point3d ptX0 = new Point3d(H + OffX, -W + OffY, 0);
                                                Point3d ptX1 = new Point3d(H + OffX, -W + OffY + dX, 0);
                                                Point3d ptDL = new Point3d(H + OffX + 0.1, OffY, 0);
                                                AlignedDimension pAligDimX = CreateAlignedDim(ptX0, ptX1, ptDL, pSInfo.s_YBJ.ToString());
                                                btr.AppendEntity(pAligDimX);
                                            }

                                            //左边距标注
                                            double dZ = double.Parse(pSInfo.s_XBJ) / 1000;
                                            if (dZ > 0)
                                            {
                                                Point3d ptZ0 = new Point3d(OffX, OffY, 0);
                                                Point3d ptZ1 = new Point3d(OffX + dZ, OffY, 0);
                                                Point3d ptDL = new Point3d(OffX, OffY + 0.1, 0);
                                                AlignedDimension pAligDimZ = CreateAlignedDim(ptZ0, ptZ1, ptDL, pSInfo.s_XBJ.ToString());
                                                btr.AppendEntity(pAligDimZ);
                                            }
                                            //右边距标注
                                            double dY = double.Parse(pSInfo.s_SBJ) / 1000;
                                            if (dY > 0)
                                            {
                                                Point3d ptY0 = new Point3d(H + OffX, OffY, 0);
                                                Point3d ptY1 = new Point3d(H + OffX - dY, OffY, 0);
                                                Point3d ptDL = new Point3d(H + OffX, OffY + 0.1, 0);
                                                AlignedDimension pAligDimY = CreateAlignedDim(ptY0, ptY1, ptDL, pSInfo.s_SBJ.ToString());
                                                btr.AppendEntity(pAligDimY);
                                            }

                                            //添加孔位
                                            if (pSInfo.m_DicKW != null && pSInfo.m_DicKW.Count > 0)
                                            {

                                                double hJJ = (double.Parse(pWInfo.w_JS) - (int.Parse(pSInfo.s_H) * pSInfo.s_maxGJ) - double.Parse(pSInfo.s_SBJ) - double.Parse(pSInfo.s_XBJ)) / (int.Parse(pSInfo.s_H) - 1);
                                                double lJJ = (double.Parse(pWInfo.w_JK) - (int.Parse(pSInfo.s_L) * pSInfo.s_maxGJ) - double.Parse(pSInfo.s_ZBJ) - double.Parse(pSInfo.s_YBJ)) / (int.Parse(pSInfo.s_L) - 1);
                                                hJJ = hJJ / 1000;
                                                lJJ = lJJ / 1000;
                                                if(pSInfo.s_H=="1")
                                                {
                                                    hJJ = 0;
                                                }
                                                if (pSInfo.s_L == "1")
                                                {
                                                    lJJ = 0;
                                                }

                                                Dictionary<string, Dictionary<string, KWD>> DicKW = new Dictionary<string, Dictionary<string, KWD>>();

                                                int mid = 0;
                                                if (int.Parse(pSInfo.s_H) <= 3)
                                                {
                                                    mid = int.Parse(pSInfo.s_H);
                                                }
                                                else
                                                {
                                                    if (int.Parse(pSInfo.s_H) % 2 == 0)
                                                    {
                                                        mid = int.Parse(pSInfo.s_H) / 2;
                                                    }
                                                    else if (int.Parse(pSInfo.s_H) % 2 == 1)
                                                    {
                                                        mid = int.Parse(pSInfo.s_H) / 2 + 1;
                                                    }
                                                }

                                                double x0 = OffX + double.Parse(pSInfo.s_XBJ) / 1000;
                                                double y0 = OffY - double.Parse(pSInfo.s_ZBJ) / 1000;
                                                double dGJ = pSInfo.s_maxGJ / 2000;

                                                for (int m = int.Parse(pSInfo.s_H)-1; m >= 0; m--)
                                                {
                                                    y0 = OffY - double.Parse(pSInfo.s_ZBJ) / 1000;
                                                    x0 = x0 + dGJ;
                 
                                                    for (int n = 0; n < int.Parse(pSInfo.s_L); n++)
                                                    {
                                                        y0 = y0 - dGJ;
                                                        string strCurHL = m.ToString() + "_" + n.ToString();
                                                        if (!pSInfo.m_DicKW.Keys.Contains(strCurHL))
                                                        {
                                                            y0 = y0 - dGJ - lJJ;
                                                            continue;
                                                        }

                                                        KongWeiInfo pHInfo = pSInfo.m_DicKW[strCurHL];
                                                        if (pHInfo.h_GJ == "")
                                                        {
                                                            y0 = y0 - dGJ - lJJ;
                                                            continue;
                                                        }


                                                        Point3d ptKW = new Point3d(x0, y0, 0);
                                                        string strName = pHInfo.h_LX + pHInfo.h_GJ;
                                                        ObjectId KId = ObjectId.Null;
                                                        if (m_dicSelBlk.Keys.Contains(strName))
                                                        {
                                                            KId = m_dicSelBlk[strName];
                                                        }
                                                        else
                                                        {
                                                            KId = UtilityFunc.GetBlkIdByName(pDb, strName);
                                                            m_dicSelBlk.Add(strName, KId);
                                                        }

                                                        if (KId == ObjectId.Null)
                                                        {
                                                            y0 = y0 - dGJ - lJJ;
                                                            continue;
                                                        }
                                                   
                                                        BlockReference blkRefKW = new BlockReference(ptKW, KId);
                                                        btr.AppendEntity(blkRefKW);

                                                        string strLead = pHInfo.h_LX + pHInfo.h_CZ + pHInfo.h_GJ;
                                                        if (!dicBlk.Keys.Contains(strLead))
                                                        {
                                                            dicBlk.Add(strLead, KId);
                                                        }

                                                        //绘制孔位标注

                                                        KWD p = new KWD();
                                                        p.pt = ptKW;
                                                        p.str = pHInfo.h_XCBH;

                                                        if (DicKW.Keys.Contains(m.ToString()))
                                                        {
                                                            Dictionary<string, KWD> dic = DicKW[m.ToString()];
                                                            dic.Add(n.ToString(), p);
                                                            DicKW[m.ToString()] = dic;
                                                        }
                                                        else
                                                        {
                                                            Dictionary<string, KWD> dic = new Dictionary<string, KWD>();
                                                            dic.Add(n.ToString(), p);
                                                            DicKW.Add(m.ToString(), dic);
                                                        }

                                                        y0 = y0 - dGJ - lJJ;
                                                    }

                                                    x0 = x0 + dGJ + hJJ;
                                                
                                                }

                                                //孔位标注
                                                double xOff = 4;
                                                double yOff = 0.5;

                                                List<string> listH = new List<string>(DicKW.Keys);

                                                for (int i = listH.Count - 1; i >= 0; i--)
                                                {
                                                    xOff = xOff + dGJ;
                                                    Dictionary<string, KWD> dic = DicKW[listH[i]];

                                                    List<string> listL = new List<string>(dic.Keys);

                                                    for (int j = 0; j < listL.Count; j++)
                                                    {
                                                        KWD p = dic[listL[j]];

                                                        if (p.str == "" || p.str == "无")
                                                        {
                                                            continue;
                                                        }


                                                        if (listH.Count > mid && i < mid)
                                                        {
                                                            Point2dCollection ptColt = new Point2dCollection();
                                                            ptColt.Add(new Point2d(p.pt.X, p.pt.Y));
                                                            ptColt.Add(new Point2d(p.pt.X, -OffY - yOff));
                                                            ptColt.Add(new Point2d(p.pt.X + xOff, -OffY - yOff));

                                                            Teigha.DatabaseServices.Polyline pLBH = CreatePLineKW(ptColt);
                                                            DBText textBH = CreateText(new Point3d(p.pt.X, -OffY - yOff - 0.10, 0), p.str, "井注记7");
                                                            btr.AppendEntity(pLBH);
                                                            textBH.HorizontalMode = TextHorizontalMode.TextLeft;
                                                            btr.AppendEntity(textBH);
                                                        }
                                                        else
                                                        {

                                                            Point2dCollection ptColt = new Point2dCollection();
                                                            ptColt.Add(new Point2d(p.pt.X, p.pt.Y));
                                                            ptColt.Add(new Point2d(p.pt.X, OffY + yOff));
                                                            ptColt.Add(new Point2d(p.pt.X + xOff, OffY + yOff));

                                                            Teigha.DatabaseServices.Polyline pLBH = CreatePLineKW(ptColt);
                                                            DBText textBH = CreateText(new Point3d(p.pt.X, OffY + yOff + 0.10, 0), p.str, "井注记7");
                                                            btr.AppendEntity(pLBH);
                                                            textBH.HorizontalMode = TextHorizontalMode.TextLeft;
                                                            btr.AppendEntity(textBH);

                                                        }
                                                        yOff = yOff + 0.3;
                                                    }

                                                    xOff = xOff + dGJ + hJJ;
                                                }

                                            }

                                        }
                                        else if (pSInfo.s_FW == "西")
                                        {
                                            OffX = H + L / 2 + dJJ;
                                            OffY = W / 2;
                                            pSWell = CreatePLine(H, W, -OffX, OffY);

                                            //上边距标注
                                            double dS = double.Parse(pSInfo.s_YBJ) / 1000;
                                            if (dS > 0)
                                            {
                                                Point3d ptS0 = new Point3d(-OffX, OffY, 0);
                                                Point3d ptS1 = new Point3d(-OffX, OffY - dS, 0);
                                                Point3d ptDL = new Point3d(-OffX - 0.1, OffY, 0);
                                                AlignedDimension pAligDimS = CreateAlignedDim(ptS0, ptS1, ptDL, pSInfo.s_YBJ.ToString());
                                                btr.AppendEntity(pAligDimS);
                                            }

                                            //下边距标注
                                            double dX = double.Parse(pSInfo.s_ZBJ) / 1000;
                                            if (dX > 0)
                                            {
                                                Point3d ptX0 = new Point3d(-OffX, -W + OffY, 0);
                                                Point3d ptX1 = new Point3d(-OffX, -W + OffY + dX, 0);
                                                Point3d ptDL = new Point3d(-OffX - 0.1, OffY, 0);
                                                AlignedDimension pAligDimX = CreateAlignedDim(ptX0, ptX1, ptDL, pSInfo.s_ZBJ.ToString());
                                                btr.AppendEntity(pAligDimX);
                                            }

                                            //左边距标注
                                            double dZ = double.Parse(pSInfo.s_SBJ) / 1000;
                                            if (dZ > 0)
                                            {
                                                Point3d ptZ0 = new Point3d(-OffX, OffY, 0);
                                                Point3d ptZ1 = new Point3d(-OffX + dZ, OffY, 0);
                                                Point3d ptDL = new Point3d(-OffX, OffY + 0.1, 0);
                                                AlignedDimension pAligDimZ = CreateAlignedDim(ptZ0, ptZ1, ptDL, pSInfo.s_SBJ.ToString());
                                                btr.AppendEntity(pAligDimZ);
                                            }
                                            //右边距标注
                                            double dY = double.Parse(pSInfo.s_XBJ) / 1000;
                                            if (dY > 0)
                                            {
                                                Point3d ptY0 = new Point3d(H - OffX, OffY, 0);
                                                Point3d ptY1 = new Point3d(H - OffX - dY, OffY, 0);
                                                Point3d ptDL = new Point3d(H - OffX, OffY + 0.1, 0);
                                                AlignedDimension pAligDimY = CreateAlignedDim(ptY0, ptY1, ptDL, pSInfo.s_XBJ.ToString());
                                                btr.AppendEntity(pAligDimY);
                                            }

                                            //添加孔位
                                            if (pSInfo.m_DicKW != null && pSInfo.m_DicKW.Count > 0)
                                            {
                                                double hJJ = (double.Parse(pWInfo.w_JS) - (int.Parse(pSInfo.s_H) * pSInfo.s_maxGJ) - double.Parse(pSInfo.s_SBJ) - double.Parse(pSInfo.s_XBJ)) / (int.Parse(pSInfo.s_H) - 1);
                                                double lJJ = (double.Parse(pWInfo.w_JK) - (int.Parse(pSInfo.s_L) * pSInfo.s_maxGJ) - double.Parse(pSInfo.s_ZBJ) - double.Parse(pSInfo.s_YBJ)) / (int.Parse(pSInfo.s_L) - 1);
                                                hJJ = hJJ / 1000;
                                                lJJ = lJJ / 1000;

                                                if (pSInfo.s_H == "1")
                                                {
                                                    hJJ = 0;
                                                }
                                                if (pSInfo.s_L == "1")
                                                {
                                                    lJJ = 0;
                                                }

                                                //孔位标注
                                                double xOff = 4;
                                                double yOff = 0.5;

                                                Dictionary<string, Dictionary<string, KWD>> DicKW = new Dictionary<string, Dictionary<string, KWD>>();

                                                int mid = 0;
                                                if (int.Parse(pSInfo.s_H) <= 3)
                                                {
                                                    mid = int.Parse(pSInfo.s_H);
                                                }
                                                else
                                                {
                                                    if (int.Parse(pSInfo.s_H) % 2 == 0)
                                                    {
                                                        mid = int.Parse(pSInfo.s_H) / 2;
                                                    }
                                                    else if (int.Parse(pSInfo.s_H) % 2 == 1)
                                                    {
                                                        mid = int.Parse(pSInfo.s_H) / 2 + 1;
                                                    }
                                                }


                                                double x0 = -OffX + double.Parse(pSInfo.s_SBJ) / 1000;
                                                double y0 = OffY - double.Parse(pSInfo.s_YBJ) / 1000;
                                                double dGJ = pSInfo.s_maxGJ / 2000;

                                                for (int m = 0; m <= int.Parse(pSInfo.s_H); m++)
                                                {
                                                    y0 = OffY - double.Parse(pSInfo.s_YBJ) / 1000;
                                                    x0 = x0 + dGJ;
                                                    xOff = xOff + dGJ;
                                                    for (int n = int.Parse(pSInfo.s_L) - 1; n >= 0; n--)
                                                    {
                                                        y0 = y0 - dGJ;

                                                        string strCurHL = m.ToString() + "_" + n.ToString();
                                                        if (!pSInfo.m_DicKW.Keys.Contains(strCurHL))
                                                        {
                                                            y0 = y0 - dGJ - lJJ;
                                                            continue;
                                                        }

                                                        KongWeiInfo pHInfo = pSInfo.m_DicKW[strCurHL];
                                                        if (pHInfo.h_GJ == "")
                                                        {
                                                            y0 = y0 - dGJ - lJJ;
                                                            continue;
                                                        }

                                                      
                                                        Point3d ptKW = new Point3d(x0, y0, 0);
                                                        string strName = pHInfo.h_LX + pHInfo.h_GJ;
                                                        ObjectId KId = ObjectId.Null;
                                                        if (m_dicSelBlk.Keys.Contains(strName))
                                                        {
                                                            KId = m_dicSelBlk[strName];
                                                        }
                                                        else
                                                        {
                                                            KId = UtilityFunc.GetBlkIdByName(pDb, strName);
                                                            m_dicSelBlk.Add(strName, KId);
                                                        }

                                                        if (KId == ObjectId.Null)
                                                        {
                                                            y0 = y0 - dGJ - lJJ;
                                                            continue;
                                                        }
                                                        BlockReference blkRefKW = new BlockReference(ptKW, KId);
                                                        btr.AppendEntity(blkRefKW);

                                                        string strLead = pHInfo.h_LX + pHInfo.h_CZ + pHInfo.h_GJ;
                                                        if (!dicBlk.Keys.Contains(strLead))
                                                        {
                                                            dicBlk.Add(strLead, KId);
                                                        }

                                                        //绘制孔位标注
                                                        if (m > mid)
                                                        {
                                                            KWD p = new KWD();
                                                            p.pt = ptKW;
                                                            p.str = pHInfo.h_XCBH;

                                                            if (DicKW.Keys.Contains(m.ToString()))
                                                            {
                                                                Dictionary<string, KWD> dic = DicKW[m.ToString()];
                                                                dic.Add(n.ToString(), p);
                                                                DicKW[m.ToString()] = dic;
                                                            }
                                                            else
                                                            {
                                                                Dictionary<string, KWD> dic = new Dictionary<string, KWD>();
                                                                dic.Add(n.ToString(), p);
                                                                DicKW.Add(m.ToString(), dic);
                                                            }

                                                        }
                                                        else if (m <= mid &&(pHInfo.h_XCBH != ""&& pHInfo.h_XCBH != "无"))
                                                        {

                                                            Point2dCollection ptColt = new Point2dCollection();
                                                            ptColt.Add(new Point2d(x0, y0));
                                                            ptColt.Add(new Point2d(x0, OffY+yOff));
                                                            ptColt.Add(new Point2d(x0-xOff, OffY + yOff));

                                                            Teigha.DatabaseServices.Polyline pLBH = CreatePLineKW(ptColt);
                                                            DBText textBH = CreateText(new Point3d(x0 - xOff, OffY + yOff+0.10, 0), pHInfo.h_XCBH, "井注记7");
                                                            btr.AppendEntity(pLBH);
                                                            textBH.HorizontalMode = TextHorizontalMode.TextLeft;
                                                            btr.AppendEntity(textBH);

                                                            yOff = yOff + 0.3;
                                                        }

                                                        y0 = y0 - dGJ - lJJ;
                                                    }

                                                    x0 = x0 + dGJ + hJJ;
                                                    xOff = xOff + dGJ + hJJ;
                                                }

                                                xOff = 4;
                                                yOff = 0.5;

                                                List<string> listH = new List<string>(DicKW.Keys);

                                                for (int i = listH.Count - 1; i >= 0; i--)
                                                {
                                                    xOff = xOff + dGJ;
                                                    Dictionary<string, KWD> dic = DicKW[listH[i]];

                                                    List<string> listL = new List<string>(dic.Keys);

                                                    for (int j = 0; j < listL.Count; j++)
                                                    {
                                                        KWD p = dic[listL[j]];

                                                        if (p.str == ""|| p.str == "无")
                                                        {
                                                            continue;
                                                        }

                                                        Point2dCollection ptColt = new Point2dCollection();
                                                        ptColt.Add(new Point2d(p.pt.X, p.pt.Y));
                                                        ptColt.Add(new Point2d(p.pt.X, -OffY - yOff));
                                                        ptColt.Add(new Point2d(p.pt.X + xOff, -OffY - yOff));

                                                        Teigha.DatabaseServices.Polyline pLBH = CreatePLineKW(ptColt);
                                                        DBText textBH = CreateText(new Point3d(p.pt.X + xOff, -OffY - yOff - 0.10, 0), p.str, "井注记7");
                                                        btr.AppendEntity(pLBH);
                                                        textBH.HorizontalMode = TextHorizontalMode.TextLeft;
                                                        btr.AppendEntity(textBH);

                                                        yOff = yOff + 0.3;
                                                    }

                                                    xOff = xOff + dGJ + hJJ;
                                                }

                                            }

                                        }

                                        ObjectId sId = btr.AppendEntity(pSWell);
                                        Entity psEnt = (Entity)sId.Open(OpenMode.ForWrite);
                                        psEnt.Layer = "井剖面";

                                        psEnt.Color = Color.FromColor(System.Drawing.Color.Red);

                                    }

                                    //创建图例

                                    double x = -6.2;
                                    double y = -4.1;
                                    foreach (string strLead in dicBlk.Keys)
                                    {
                                        y = y - 0.2;
                                        ObjectId id = dicBlk[strLead];

                                        Point3d pt = new Point3d(x, y, 0);
                                        BlockReference blkRefLead = new BlockReference(pt, id);
                                        btr.AppendEntity(blkRefLead);

                                        double y0 = y + 0.07;
                                        double x0 = x + 0.07;
                                        Point3d ptText = new Point3d(x0, y0, 0);

                                        DBText pText = CreateText(ptText, strLead, "井注记7");
                                        pText.VerticalMode = TextVerticalMode.TextBottom;
                                        pText.HorizontalMode = TextHorizontalMode.TextLeft;
                                        pText.Height = 0.04;
                                        btr.AppendEntity(pText);

                                        Teigha.DatabaseServices.Polyline pPLine = new Teigha.DatabaseServices.Polyline();

                                        pPLine.AddVertexAt(0, new Point2d(x, y), 0, 0, 0);
                                        pPLine.AddVertexAt(1, new Point2d(x + 0.07, y + 0.07), 0, 0, 0);
                                        pPLine.AddVertexAt(2, new Point2d(x + 0.07 + strLead.Length * 0.04, y + 0.07), 0, 0, 0);
                                        btr.AppendEntity(pPLine);

                                    }
                                    //创建方位拓扑
                                    //创建方位矩形                            
                                    Teigha.DatabaseServices.Polyline pL = CreatePLine(0.5, 0.5, 6.38, 5.23);
                                    ObjectId tpId = btr.AppendEntity(pL);

                                    foreach (string strFW in dicTP.Keys)
                                    {
                                        string strTP = dicTP[strFW];
                                        if (strFW == "北")
                                        {
                                            Point3d ptB = new Point3d(6.63, 5.23, 0);

                                            ObjectId BId = ObjectId.Null;
                                            if (m_dicSelBlk.Keys.Contains("FXB"))
                                            {
                                                BId = m_dicSelBlk["FXB"];
                                            }
                                            else
                                            {
                                                BId = UtilityFunc.GetBlkIdByName(pDb, "FXB");
                                                m_dicSelBlk.Add("FXB", BId);
                                            }

                                            BlockReference blkRefFX = new BlockReference(ptB, BId);
                                            btr.AppendEntity(blkRefFX);

                                            DBText pText = CreateText(new Point3d(6.63, 5.98, 0), strTP, "井注记1");
                                            pText.VerticalMode = TextVerticalMode.TextBottom;
                                            pText.HorizontalMode = TextHorizontalMode.TextCenter;
                                            pText.Height = 0.1869;
                                            btr.AppendEntity(pText);
                                        }
                                        else if (strFW == "南")
                                        {
                                            Point3d ptN = new Point3d(6.63, 4.73, 0);

                                            ObjectId NId = ObjectId.Null;
                                            if (m_dicSelBlk.Keys.Contains("FXN"))
                                            {
                                                NId = m_dicSelBlk["FXN"];
                                            }
                                            else
                                            {
                                                NId = UtilityFunc.GetBlkIdByName(pDb, "FXN");
                                                m_dicSelBlk.Add("FXN", NId);
                                            }

                                            BlockReference blkRefFX = new BlockReference(ptN, NId);
                                            btr.AppendEntity(blkRefFX);

                                            DBText pText = CreateText(new Point3d(6.63, 3.98, 0), strTP, "井注记1");
                                            pText.VerticalMode = TextVerticalMode.TextTop;
                                            pText.HorizontalMode = TextHorizontalMode.TextCenter;
                                            pText.Height = 0.1869;
                                            btr.AppendEntity(pText);
                                        }
                                        else if (strFW == "东")
                                        {
                                            Point3d ptD = new Point3d(6.88, 4.98, 0);

                                            ObjectId DId = ObjectId.Null;
                                            if (m_dicSelBlk.Keys.Contains("FXD"))
                                            {
                                                DId = m_dicSelBlk["FXD"];
                                            }
                                            else
                                            {
                                                DId = UtilityFunc.GetBlkIdByName(pDb, "FXD");
                                                m_dicSelBlk.Add("FXD", DId);
                                            }

                                            BlockReference blkRefFX = new BlockReference(ptD, DId);
                                            btr.AppendEntity(blkRefFX);

                                            DBText pText = CreateText(new Point3d(7.63, 4.98, 0), strTP, "井注记1");
                                            pText.VerticalMode = TextVerticalMode.TextVerticalMid;
                                            pText.HorizontalMode = TextHorizontalMode.TextLeft;
                                            pText.Height = 0.1869;
                                            btr.AppendEntity(pText);
                                        }
                                        else if (strFW == "西")
                                        {
                                            Point3d ptX = new Point3d(6.38, 4.98, 0);

                                            ObjectId XId = ObjectId.Null;
                                            if (m_dicSelBlk.Keys.Contains("FXX"))
                                            {
                                                XId = m_dicSelBlk["FXX"];
                                            }
                                            else
                                            {
                                                XId = UtilityFunc.GetBlkIdByName(pDb, "FXX");
                                                m_dicSelBlk.Add("FXX", XId);
                                            }

                                            BlockReference blkRefFX = new BlockReference(ptX, XId);
                                            btr.AppendEntity(blkRefFX);

                                            DBText pText = CreateText(new Point3d(5.63, 4.98, 0), strTP, "井注记1");
                                            pText.VerticalMode = TextVerticalMode.TextVerticalMid;
                                            pText.HorizontalMode = TextHorizontalMode.TextRight;
                                            pText.Height = 0.1869;
                                            btr.AppendEntity(pText);
                                        }

                                    }
                                }
                            }
                        }
                        newBlkId = wBlockId;
                    }
                }
            }
            catch (System.Exception ex)
            {

            }
            return newBlkId;
        }

        private Teigha.DatabaseServices.Polyline CreatePLineKW(Point2dCollection ptColt)
        {
            Teigha.DatabaseServices.Polyline pPLine = new Teigha.DatabaseServices.Polyline();
            try
            {
                int nCount = ptColt.Count;
                for (int i = 0;i<nCount;i++)
                {
                    Point2d pt = ptColt[i];

                    pPLine.AddVertexAt(i, pt, 0, 0, 0);
                }             

            }
            catch (System.Exception ex)
            {

            }

            return pPLine;

        }

        private Teigha.DatabaseServices.Polyline CreatePLine(double L,double W, double OffX,double OffY)
       {
            Teigha.DatabaseServices.Polyline pPLine = new Teigha.DatabaseServices.Polyline() ;
            try
            {
 
                Point2d pt = Point2d.Origin;
               
                pPLine.AddVertexAt(0, new Point2d(OffX, OffY), 0, 0, 0);
                pPLine.AddVertexAt(1, new Point2d(L + OffX, OffY), 0, 0, 0);
                pPLine.AddVertexAt(2, new Point2d(L + OffX, -W + OffY), 0, 0, 0);
                pPLine.AddVertexAt(3, new Point2d(OffX, -W + OffY), 0,0, 0);
               
                pPLine.Closed = true;
               
            }
            catch (System.Exception ex)
            {

            }

            return pPLine;
            
       }

        private ObjectId GetDimStyleIdByName(Database pDb, string strName)
        {
            ObjectId Oid = ObjectId.Null;
            try
            {
                using (DimStyleTable styleTable = (DimStyleTable)pDb.DimStyleTableId.GetObject(OpenMode.ForRead))
                {
                    foreach (ObjectId id in styleTable)
                    {
                        using (DimStyleTableRecord pDimStyle = (DimStyleTableRecord)id.Open(OpenMode.ForRead))
                        {
                            if (pDimStyle.Name == strName)
                            {
                                Oid = id;
                                break;
                            }
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {

            }
            return Oid;
        }

        private AlignedDimension CreateAlignedDim(Point3d ptStart, Point3d ptEnd, Point3d ptDimLine, string strText)
       {
            AlignedDimension pAligDim = new AlignedDimension();
            pAligDim.XLine1Point = ptStart;
            pAligDim.XLine2Point = ptEnd;
            pAligDim.DimensionText = strText;

            ObjectId id = GetDimStyleIdByName(m_pDb, "PMBZ");

            pAligDim.DimensionStyle = id;
            pAligDim.Oblique = 0;
           
            pAligDim.TextAttachment = AttachmentPoint.MiddleCenter;
            pAligDim.TextLineSpacingStyle = LineSpacingStyle.AtLeast;

            pAligDim.DimLinePoint = ptDimLine;

            return pAligDim;
       }

        private ObjectId GetTextStyleIdByName(Database pDb, string strName)
        {
            ObjectId Oid = ObjectId.Null;
            try
            {
                using (TextStyleTable styleTable = (TextStyleTable)pDb.TextStyleTableId.GetObject(OpenMode.ForRead))
                {
                    foreach (ObjectId id in styleTable)
                    {
                        using (TextStyleTableRecord pTextStyle = (TextStyleTableRecord)id.Open(OpenMode.ForRead))
                        {
                            if (pTextStyle.Name == strName)
                            {
                                Oid = id;
                                break;
                            }
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {

            }
            return Oid;
        }

        private DBText CreateText(Point3d pt,string strText,string strTextStyleName)
        {
            DBText pText = new DBText();

            pText.TextString = strText;

            ObjectId styleId = GetTextStyleIdByName(m_pDb, strTextStyleName);
            pText.TextStyleId = styleId;
            pText.Height = 0.12;
            pText.Rotation = 0;
            pText.WidthFactor = 1;
            pText.Oblique = 0;
            pText.VerticalMode = TextVerticalMode.TextVerticalMid;
            pText.HorizontalMode = TextHorizontalMode.TextCenter;
          
            pText.AlignmentPoint = pt;
            pText.Position = pt;

            return pText;
        }

        private bool CreateAttribute(Database pDb, JingInfo pWInfo, BlockTableRecord btr)
        {
            try
            {
                Point3d pt1 = new Point3d(7.2,-3.0,0);
                string str1 = pWInfo.w_JSL + "块" + pWInfo.w_XZ + "井盖";
                DBText text1 = CreateText(pt1, str1,"井注记2");
                btr.AppendEntity(text1);

                Point3d pt2 = new Point3d(7.2, -3.39, 0);
                string str2 = pWInfo.w_GBCC;
                DBText text2 = CreateText(pt2, str2, "井注记2");
                btr.AppendEntity(text2);

                Point3d pt3 = new Point3d(7.2, -3.81, 0);
                string str3 = pWInfo.w_CZ;
                DBText text3 = CreateText(pt3, str3, "井注记2");
                btr.AppendEntity(text3);

                Point3d pt4 = new Point3d(7.2, -4.13, 0);
                string str4 = pWInfo.w_JSDS;
                DBText text4 = CreateText(pt4, str4, "井注记2");
                btr.AppendEntity(text4);

                Point3d pt5 = new Point3d(8.2, -6.40, 0);
                string str5 = pWInfo.w_MC;
                DBText text5 = CreateText(pt5, str5, "井注记2");
                btr.AppendEntity(text5);

                Point3d pt6 = new Point3d(2.48, -6.35, 0);
                string str6 = "韦如强";
                DBText text6 = CreateText(pt6, str6, "井注记2");
                btr.AppendEntity(text6);

                Point3d pt7 = new Point3d(2.44, -7.0, 0);
                string str7 = "段伟国";
                DBText text7 = CreateText(pt7, str7, "井注记2");
                btr.AppendEntity(text7);

                 pt7 = new Point3d(4.7, -6.4, 0);
                 str7 = "段伟国";
                 text7 = CreateText(pt7, str7, "井注记2");
                btr.AppendEntity(text7);

                pt7 = new Point3d(4.67, -7.0, 0);
                str7 = "张超";
                text7 = CreateText(pt7, str7, "井注记2");
                btr.AppendEntity(text7);

                pt7 = new Point3d(8.15, -7.0, 0);
                str7 = pWInfo.w_RQ;
                text7 = CreateText(pt7, str7, "井注记2");
                btr.AppendEntity(text7);

                Point3d pt = new Point3d(6.0, -5.65, 0);
                string str = "沈阳地下电缆普查项目";
                DBText text = CreateText(pt, str, "井注记2");
                btr.AppendEntity(text);
            }
            catch(System.Exception ex)
            {

            }

            return true;
        }

        private bool CreateTongDaoX(Database pDb, string str, Dictionary<int, Dictionary<int, SheBeiInfo>> dicTD, BlockTableRecord btr)
        {
           
            try
            {
               

                foreach (int iTDID in dicTD.Keys)
                {
                    Dictionary<int, SheBeiInfo> dicSB = dicTD[iTDID];

                    Dictionary<int, SheBeiInfo> dicAsc = dicSB.OrderBy(o => o.Key).ToDictionary(o => o.Key, p => p.Value);

                    Teigha.DatabaseServices.Polyline pPLine = new Teigha.DatabaseServices.Polyline();

                    double x = 0;
                    double y = 0;

                    int i = 0;

                    foreach (int iXH in dicAsc.Keys)
                    {
                        SheBeiInfo pInfo = dicAsc[iXH];

                        ConvertPlaneCoord.MCT84Bl2xy(double.Parse(pInfo.s_X), double.Parse(pInfo.s_Y), out x, out y);
                        Point2d ptS = new Point2d(y, x);
                        pPLine.AddVertexAt(i, ptS, 0, 0, 0);

                        i++;

                    }

                    ObjectId Id = btr.AppendEntity(pPLine);


                    if (!string.IsNullOrEmpty(str) && UtilityFunc.FindCADLayerByNM(pDb, str) == true)
                    {
                        Entity pwEnt = (Entity)Id.Open(OpenMode.ForWrite);
                        pwEnt.Layer = str;
                    }

                }

              

            }
            catch(System.Exception ex)
            {

            }

            return true;
        }

        private ObjectId GetLineStyleIdByName(Database pDb, string strName)
        {
            ObjectId Oid = ObjectId.Null;
            try
            {
                using (LinetypeTable styleTable = (LinetypeTable)pDb.LinetypeTableId.GetObject(OpenMode.ForRead))
                {
                    foreach (ObjectId id in styleTable)
                    {
                        using (LinetypeTableRecord pLineStyle = (LinetypeTableRecord)id.Open(OpenMode.ForRead))
                        {
                            if (pLineStyle.Name == strName)
                            {
                                Oid = id;
                                break;
                            }
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {

            }
            return Oid;
        }

        private MlineStyle CreateMLineStyle()
       {
            MlineStyle mlStyle = new MlineStyle();
            try
            {
                mlStyle.Name = "通道";
                MlineStyleElementCollection Elements = mlStyle.Elements;
                for(int i =0;i<Elements.Count;i++)
                {
                    Elements.RemoveAt(i);
                }

                ObjectId id = GetLineStyleIdByName(m_pDb, "Continuous");
                MlineStyleElement Item0 = new MlineStyleElement(1, Color.FromColor(System.Drawing.Color.White), id) ;
                MlineStyleElement Item1 = new MlineStyleElement(-1, Color.FromColor(System.Drawing.Color.White), id);
                Elements.Add(Item0,false);
                Elements.Add(Item1,false);

            }
            catch(System.Exception ex)
            {

            }

            return mlStyle;
       }

        private ObjectId GetMLineStyleIdByName(Database pDb, string strName)
        {
            ObjectId Oid = ObjectId.Null;

            try
            {

                TransactionManager pTMgr = pDb.TransactionManager;
                using (Transaction pTransac = pTMgr.StartTransaction())
                {
                    DBDictionary pDBDic = pTMgr.GetObject(pDb.MLStyleDictionaryId, OpenMode.ForRead) as DBDictionary;
                    Oid = pDBDic.GetAt(strName);

                 
                    pTransac.Commit();
                }

             
            }
            catch (System.Exception ex)
            {

            }
            return Oid;
        }


    }
}
